/*
Copyright (C) 2021-2025 Casa Xu (also Zhiyan Xu) from HIT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
 * LOS_StarSensor.h
 *
 *  Created on: 2022年7月30日
 *      Author: 64435
 */

#ifndef PERIPHERAL_LAYER_INC_LOS_STARSENSOR_H_
#define PERIPHERAL_LAYER_INC_LOS_STARSENSOR_H_

#include "LOS_CAN.h"

#define StarSensorY nID_StarSensorY
#define StarSensorZ nID_StarSensorZ

#define SSMSG_SP 0XF0  //数据简包
#define SSMSG_LP 0XF1  //数据详包    
#define SSMSG_SC 0XF2  //自检遥测
#define SSMSG_AG 0XF3  //参数下传
#define SSMSG_SM 0XF4  //星图遥测

extern uint8_t TMPACK_SSYShort[72];
extern uint8_t TMPACK_SSYLong[471];
extern uint8_t TMPACK_SSYCheck[16];
extern uint8_t TMPACK_SSYArg[112];
extern uint8_t TMPACK_SSYImage[419];

extern uint8_t TMPACK_SSZShort[72];
extern uint8_t TMPACK_SSZLong[471];
extern uint8_t TMPACK_SSZCheck[16];
extern uint8_t TMPACK_SSZArg[112];
extern uint8_t TMPACK_SSZImage[419];

typedef struct LOS_SSSTA_t
{
    /* 健康管理状态量 */
    uint8_t VAL;                //数据有效性标志，0：有效， 其他值：无效
    uint8_t CMD;                //开关机指令状态,发送开机指令后置1，发送关机指令后置0
    uint8_t PWR;                //电源状态，读取电源遥测，通电为1，断电为0
    uint8_t MSG;                //通信状态，通信正常为0，通信异常为其他值
    uint8_t RBCNT;              //复位次数
    uint8_t NFR;                //Need For Reset 需要复位标志位，0为不需要，其他值为需要复位
    uint8_t RBT_F;              //表示单机是否处于重启过程中 0：未处于 1：处于重启过程中
    uint8_t RECOV;              //恢复计数：单机重启完成后，需要等待一段时间才能再次进入重启流程，将该计数置于某一值开始倒计时，倒计时结束后会再次开始判断是否需要重启
    uint8_t SWNUM;
    uint8_t HL_TXF;             //HL_前缀表示健康管理相关变量。遥测请求发送完成标志,发送一次后该量置1，接收到回应后置0(TX FINISHED)
    uint8_t HL_WCNT;            //等待接收计数器。该计数器在操作系统的一个线程中累加，当HL_TXF为1时该值+1，HL_TXF为0时该值清0，当累加到一定值时系统会判断通信异常，并将MSG置1
    uint8_t REC;                //CAN识别标志位,0:未识别  1：识别
    uint32_t TMCNT;
    uint16_t FlagF_CNT;         //物理量异常重启计数
    uint16_t FlagT_CNT;         //通信异常重启计数
    uint16_t FlagY_CNT;         //
    /* 数据简包遥测量 */
    uint32_t timestamp_sec;     //秒时间戳
    uint16_t timestamp_milsec;  //毫秒时间戳
    double timestamp;           //小数时间戳
    uint32_t q_o[4];            //四元数源码
    uint32_t angvel_o[3];       //角速度源码
    double q[4];                //星敏输出姿态四元数
    double angvel[3];           //三轴角速度
    uint8_t MODEA[4];           //工作状态A
    uint8_t MODEB[4];           //工作状态B
    uint8_t MODEC[4];           //工作状态C
    uint8_t StarNum;            //观测星个数
    uint8_t Flag_D;             //AOCS需要的Flag_D
    
    /* 自检模式遥测量 */
    int8_t temp;                //成像器件温度，单位摄氏度
    uint8_t scsta[3];           //自检状态字
    /* 星务使用状态量 */
    uint8_t FSM;                //状态机变量，用于星敏初始化

}LOS_SSSTA_t;

typedef struct LOS_SSTEST_t
{
    uint8_t q_sel;       //四元数选择：0:q0  1:q1  2:q2  3:q3  4:不作用/恢复  5:改变生效
    uint8_t angvel_sel;  //角速度选择：0:不作用/恢复  1:X轴  2:Y轴  3:Z轴  4:改变生效
    float q_upt[4];
    float angvel_upt[3];
    uint8_t star_sel;    //星个数选择：0:不作用/恢复  1:改变生效
    uint8_t StarNum_upt; //星个数上注                 
}LOS_SSTEST_t;

extern LOS_SSSTA_t STA_SSY;
extern LOS_SSSTA_t STA_SSZ;
LOS_SSTEST_t TEST_SSY;
LOS_SSTEST_t TEST_SSZ;

uint8_t LOS_StarSensor_CMD(uint8_t StarSensor_NUM, uint8_t* CMD);
uint8_t LOS_StarSensor_SetTime(uint8_t StarSensor_NUM, uint8_t* arg);
uint8_t LOS_StarSensor_IMG(uint8_t StarSensor_NUM, uint8_t* arg);
void LOS_CAN_SSYCallBack(LOS_CANMSG_t* can_re);
void LOS_CAN_SSZCallBack(LOS_CANMSG_t* can_re);
void LOS_SSY_PWROn();
void LOS_SSY_PWROff();
void LOS_SSZ_PWROn();
void LOS_SSZ_PWROff();



extern uint8_t StarCMD_Reset[7];        //星敏复位
extern uint8_t StarCMD_Check[7];        //星敏自检，会在10s内回复状态遥测
extern uint8_t StarCMD_Standby[7];      //星敏待机
extern uint8_t StarCMD_Attitude[7];     //进入姿态测量模式
extern uint8_t StarCMD_Short[7];        //请求数据简包
extern uint8_t StarCMD_Long[7];         //请求数据详包
extern uint8_t StarCMD_PAR[7];          //参数下传
extern uint8_t StarCMD_Correct[7]; 

#endif /* PERIPHERAL_LAYER_INC_LOS_STARSENSOR_H_ */
